<?php
/**
 * @author andery <andery@foxmail.com>
 * @link http://www.Nimo.com/
 * @version $Id: Nimo.php 2009-07-22 06:20:21Z andery $
 */
defined('NM_BEGIN_TIME') or define('NM_BEGIN_TIME', microtime()); //启动时间。
defined('NM_PATH') or define('NM_PATH', dirname(__FILE__)); //框架路径
defined('NM_DEBUG') or define('NM_DEBUG', true); //DUBUG开关
defined('NM_VIEW_PATH') or define('NM_VIEW_PATH', NM_PATH . '/view/system/'); //系统错误页面文件夹
defined('NM_VENDORS') or define('NM_VENDORS', NM_PATH.'/vendors'); //第三方扩展库文件夹

class Nimo
{
    private static $_app;
    
    /**
     * 创建应用程序
     * @param array 应用程序配置信息
     */
    public static function createApp($config=null)
    {
        return new CApplication($config);
    }
    
    /**
     * 获得应用程序对象
     */
    public static function app()
    {
        return self::$_app;
    }
    
    /**
     * 将应用程序赋值到属性
     */
    public static function setApp($app)
    {
        if(self::$_app===null || $app===null) {
            self::$_app=$app;
        } else {
            throw new CException(CLang::get('app_is_created'));
        }
    }
    
    /**
     * 导入类库
     * 
     */
    public static function import()
    {
        $args = func_get_args();
        if (empty($args)) {
            return false;
        }
        return array_walk($args, create_function('$value', 'include_once(NM_PATH . \'/lib/\' . $value . \'.lib.php\');'));
    }
    
    /**
     * 自动加载类文件
     * @param string 类名
     */
    public static function autoLoad($className)
    {
        if(isset(self::$_coreClasses[$className])) {
            include_once(NM_PATH . self::$_coreClasses[$className]);
        } else {
            if (!self::import($className)) {
                throw new CException(CLang::get('class_not_exists'));
                return false;
            }
        }
    }
    
    /**
     * 核心类和接口集合
     * @var array 
     */
    public static $_coreClasses = array(
        'CConf'=>'/base/CConf.php',
        'CLang'=>'/base/CLang.php',
        'CHooks'=>'/base/CHooks.php',
        'CApplication' => '/base/CApplication.php',
        'CController' => '/base/CController.php',
        'CModel' => '/base/CModel.php',
        'CView' => '/base/CView.php',
        'CTemplate' => '/view/CTemplate.php',
        'CUrlManager' => '/base/CUrlManager.php',
        'CException' => '/base/CException.php',
        'CHttpException' => '/base/CHttpException.php',
        'CDbException' => '/base/CDbException.php',
        'CDb' => '/db/CDb.php',
        'CArray' => '/lib/CArray.lib.php',
        'CArrayFile' => '/lib/CArrayFile.lib.php',
        'CCache' => '/lib/CCache.lib.php',
        'CCaptcha' => '/lib/CCaptcha.lib.php',
        'CCookie' => '/lib/CCookie.lib.php',
        'CDateTime' => '/lib/CDateTime.lib.php',
        'CFile' => '/lib/CFile.lib.php',
        'CImage' => '/lib/CImage.lib.php',
        'CIp' => '/lib/CIp.lib.php',
        'CJson' => '/lib/CJson.lib.php',
        'CMail' => '/lib/CMail.lib.php',
        'CPage' => '/lib/CPage.lib.php',
        'CRequest' => '/lib/CRequest.lib.php',
        'CSession' => '/lib/CSession.lib.php',
        'CString' => '/lib/CString.lib.php',
        'CUpload' => '/lib/CUpload.lib.php',
        'CXml' => '/lib/CXml.lib.php',
    );
    
    /**
     * 外部扩展库
     * @var array
     */
    public static $_vendors = array(
        'PHPMailer' => 'PHPMailer/class.phpmailer.php',
        'Smarty' => 'Smarty/Smarty.class.php',
    );
    
    /**
     * 获取版本号
     * @return string
     */
    public static function getVersion()
    {
        return '1.0';
    }
    
    /**
     * 版权信息
	 * @return string Powered by
	 */
    public static function powered()
    {
        return 'Powered by <a href="http://www.nimophp.com/" target="_blank">nimophp.com</a>.';
    }
}
spl_autoload_register(array('Nimo','autoLoad'));